local game = require "club.threecard.blackjack"

local seats = game.ui.element()

local log = game.logging.get("seats")


seats.store_watchers = 
{
    room = 
    {
        ["玩家进入"] = function(self, store, state, player, initialized)
            if initialized then
                for no, p in pairs(state.playing_players) do
                    self:enter(p)
                end
                return;
            end

            self:enter(player);
        end,

        ["玩家离开"] = function(self, store, state, player, initialized)
            if initialized then
                return;
            end

            self:leave(player);
        end
    },

    ingame = 
    {
        
        ["玩家下注"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            local no = args.no;
            local amount = args.amount;

            local seat = self[no];
            seat:bet(self.bet_chip, amount);
        end,

        ["玩家分牌"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            local player = args.player;
            local amount = args.amount;
            local seat = self[player.seat.no];
            seat:bet2(self.bet_chip, amount);
        end,

        ["结算"] = function(self, store, state, result_info, initialized)
            if initialized then
                return;
            end

            for _, player_info in ipairs(result_info) do
                local no = player_info.no;
                local p1 = player_info.payout1;
                local p2 = player_info.payout2;

                local seat = self[no]
                local amount = p1+p2;
                if seat and amount > 0 then
                    self.bet_chip:payout(seat, amount, function()
                        game.audio.post("blackjack:get_chips");
                        seat:payout(amount);
                    end);
                end
            end
        end,

        ["玩家购买保险"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            local player = args.player;
            local amount = args.amount;

            local seat = self[player.seat.no];
            self.bet_chip:insure(seat, amount)
        end,

        ["保险结果"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end

            if state.blackjack ~= 1 then
                return;
            end

            local claim_info = state.claim_info;
            for _, player_info in ipairs(claim_info) do
                local no = player_info.no;
                local payout = player_info.payout;
                local seat = self[no]
                if seat then
                    self.bet_chip:payout(seat, payout, function()
                        game.audio.post("blackjack:get_chips");
                        seat:payout(amount);
                    end);
                end
            end
        end
    }
}

function seats:ctor(bet_chip)
    self.res = "club.threecard.blackjack/ui/ingame.b:seats"
    self.count = 5; 
    self.bet_chip = bet_chip;

    local seat = game.require "ui.ingame.seat"
    for i = 1, self.count do
        self[i] = seat.new(i)
        self[i]:set_data(nil);
    end
end

function seats:load()
    self:super("load");
    for i = 1, self.count do
        local root = self.transform:Find("seat_"..i);
        self[i]:load(root);
        if not root then
            log.error("初始化座位信息错误  座位号："..i)
        end
    end
end

function seats:enter(player)
    if player.seat and player.seat.no > 0 then
        local seat = self[player.seat.no];
        if not seat then
            log.error("座位号越界："..player.seat.no);
            return;
        end
        game.audio.post("blackjack:sit_down");
        seat:set_data(player);
        return;
    end

    log.error(string.format("player.seat=%s no=%s", tostring(player.seat), tostring(player.seat.no)));
end

function seats:leave(player)
    if player.seat and player.seat.no > 0 then
        local seat = self[player.seat.no];
        if not seat then
            log.error("座位号越界："..player.seat.no);
            return;
        end
        game.audio.post("blackjack:stand_up");
        seat:set_data(nil);
        return;
    end

    log.error(string.format("player.seat=%s no=", tostring(player.seat), tostring(player.seat.no)));
end

function seats:clear_scene()
    for i = 1, self.count do
        local seat = self[i]
        seat:clear_scene()
    end
end

function seats:on_release()
    for i = 1, self.count do
        local seat = self[i]
        seat:release()
    end
end

return seats